package com.haoshuang.zhouzhoubang.ui.activity;


import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.haoshuang.zhouzhoubang.R;
import com.haoshuang.zhouzhoubang.utils.DownloadSaveImg;
import com.luck.picture.lib.photoview.PhotoView;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Copyright (c)
 *
 * @Description
 * @Author ""
 * @Copyright Copyright (c)
 * @Date 2017
 * @CreateBy android_studio
 */

public class LookActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    ViewPager vpView;
    ImageView btnLeft;
    TextView tvTitle;
    int currentPosition;
    List<String> imgPathsBeanList;
    LinkedList<PhotoView> mCaches = new LinkedList<PhotoView>();
    int currentImageItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_big_img);
        btnLeft = (ImageView) findViewById(R.id.iv_finish);
        btnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        vpView = (ViewPager) findViewById(R.id.vp_view);
        imgPathsBeanList = new ArrayList<>();
        imgPathsBeanList = (List<String>) getIntent().getStringArrayListExtra("imgBeen");
        currentPosition = getIntent().getIntExtra("position", 0);
        vpView.setPageMargin((int) (getResources().getDisplayMetrics().density * 15));
        vpView.setOnPageChangeListener(this);
        vpView.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imgPathsBeanList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                PhotoView view = null;
                if (mCaches.size() == 0) {
                    view = new PhotoView(LookActivity.this);
                } else {
                    view = mCaches.removeFirst();
                }
               // view.enable();
                view.setScaleType(ImageView.ScaleType.FIT_CENTER);
                String url = imgPathsBeanList.get(position);

                Glide.with(LookActivity.this)
                        .load(url)
                        .into(view);
                container.addView(view);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                view.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        DownloadSaveImg.INSTANCE.donwloadImg(LookActivity.this,url);
                        return true;
                    }
                });
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        vpView.setCurrentItem(currentPosition);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
